Annotate spot elevation with XY coordinate and change styles and settings
The below code snippet shows annotating the spot elevation with the X and Y coordinates. Also changes the text style and the DGN file settings.
//Required References
using System;
using System.Diagnostics;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.SDK;
using Bentley.DgnPlatformNET;
using Bentley.GeometryNET;
using Bentley.MstnPlatformNET;
using Bentley.TerrainModelNET;
public void AnnotateSpotElevationAndChangeSettings()
{
try
{
//The point for adding annotations
DPoint3d spot = new DPoint3d(701862.5351088, 231737.762076, 0);
//Get active DGN file
Bentley.DgnPlatformNET.DgnFile dgnFile = Session.Instance.GetActiveDgnFile();
//Get active dgn model
Bentley.DgnPlatformNET.DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
//Create connection to dgnModel
Bentley.CifNET.SDK.ConsensusConnection consensusConnection = new ConsensusConnection(dgnModel);
//Get or create text style
string textStyleName = "Line Length Label";
DgnTextStyle textStyle = DgnTextStyle.GetByName(textStyleName, dgnFile);
if (null == textStyle)
{
double dWidth = 0.00270;
double dHeight = 0.00270;
double dLineSpacing = 0.70;
string fontName = "Arial";
//Change textstyle
textStyle = new DgnTextStyle(textStyleName, dgnFile);
DgnFont dgnFont = dgnFile.GetDgnFontMap().FindFontByName(out uint foundNum, fontName, DgnFontFilterFlags.All);
textStyle.SetFontProperty(TextStyleProperty.Font, dgnFont);
textStyle.SetProperty(TextStyleProperty.Height, dHeight);
textStyle.SetProperty(TextStyleProperty.Width, dWidth);
textStyle.SetProperty(TextStyleProperty.LineSpacing, dLineSpacing);
textStyle.Add(dgnFile);
//Change settings
Settings.TextWidth = dWidth;
Settings.TextHeight = dHeight;
Settings.LineSpacing = dLineSpacing;
Settings.Font = fontName;
Settings.TextStyleName = textStyleName;
Settings.TextJustification = TextElementJustification.CenterMiddle;
dgnModel.SaveModelSettings();
}
//Get active geometric model
Bentley.CifNET.GeometryModel.SDK.GeometricModel geometricModel = consensusConnection.GetActiveGeometricModel();
if (geometricModel.ActiveSurface == null) return;
//Get active terrain
Bentley.CifNET.GeometryModel.SDK.TerrainSurface terrainSurface = geometricModel.ActiveSurface as TerrainSurface;
if (terrainSurface == null) return;
DTM dtm = terrainSurface.DTM;
Bentley.TerrainModelNET.DTMDrapedPoint drappedPoint = dtm.DrapePoint(spot);
if (null == drappedPoint || (int)Bentley.TerrainModelNET.DTMDrapedPointCode.External == drappedPoint.Code)
return;
double xCoord = drappedPoint.Coordinates.X;
double YCoord = drappedPoint.Coordinates.Y;
double elevation = drappedPoint.Coordinates.Z;
string xCoordStr = xCoord.ToString("N2");
string YCoordStr = YCoord.ToString("N2");
string elevationStr = elevation.ToString("N2");
string text = xCoordStr + "," + YCoordStr + "," + "Elevation =" + elevationStr;
//create annotation and add to model
TextBlock textBlock = new TextBlock(textStyle, dgnModel);
textBlock.AppendText(text);
textBlock.SetUserOrigin(spot);
Bentley.DgnPlatformNET.Elements.TextElement textElementCtr = (Bentley.DgnPlatformNET.Elements.TextElement)Bentley.DgnPlatformNET.Elements.TextElement.CreateElement(null, textBlock);
textElementCtr.AddToModel();
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}